package com.xms.security.service.sitemap;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xms.core.exception.XmsException;
import com.xms.datacore.service.UpdaterService;
import com.xms.security.data.PrivilegeMapper;
import com.xms.security.entity.Privilege;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * PrivilegeUpdaterService
 * </p>
 *
 * @author migo
 * @since 2021/10/2
 */
@CacheConfig(cacheNames = "privilege", keyGenerator = "CacheKeyGenerator")
@Service
public class PrivilegeUpdaterService extends UpdaterService<Privilege> implements IPrivilegeUpdaterService {

    private PrivilegeMapper _privilegeMapper;

    @Autowired
    public PrivilegeUpdaterService(PrivilegeMapper privilegeMapper) {
        _privilegeMapper = privilegeMapper;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean update(Privilege entity) throws XmsException, SQLException {
        return super.updateById(entity);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean updateMany(List<Privilege> entity) throws XmsException, SQLException {
        return false;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean update(Wrapper<Privilege> updateWrapper){
        return super.update(updateWrapper);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean update(Privilege entity, Wrapper<Privilege> updateWrapper){
        return super.update(entity, updateWrapper);
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Override
    public boolean move(UUID moveid, UUID targetid, UUID parentid, String position) {
        Privilege moveNode = getById(moveid);
        Privilege targetNode = getById(targetid);
        switch (position) {
            case "after":
                if (moveNode.getParentprivilegeid().equals(targetNode.getParentprivilegeid())) {
                    _privilegeMapper.moveAfter(moveNode.getPrivilegeid(), targetNode.getParentprivilegeid(), targetNode.getDisplayorder());
                }
                break;

            case "inside":
                if (moveNode.getParentprivilegeid().equals(targetid)) {
                    _privilegeMapper.moveInside(moveNode.getPrivilegeid(), targetid);
                }
                break;

            default:
                break;
        }
        return true;
    }

    @CacheEvict(allEntries = true, condition = "#result == true")
    @Override
    public boolean updateAuthorization(boolean state, List<UUID> id) {
        UpdateWrapper<Privilege> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(Privilege::getAuthorizationenabled, state)
                .in(Privilege::getAuthorizationenabled, id);
        return super.update(updateWrapper);
    }
}
